// order.js
var app = getApp()
var host = app.globalData.host
Page({
  data: {
    array:[],
    image: app.globalData.image,
    checked:false,
    check:true,
    arrayphoto:[],
    empty:'',
    unempty:'',
    photoempty: '',
    photounempty: ''
  },
  wedorder:function(){
    this.setData({
      checked: false,
      check: true,
    })
  },
  photoorder:function(){
    this.setData({
      checked: true,
      check: false,
    })
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '我的订单'
    })
  },
  onReady: function () {
  
  },
  refresh: function() {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: host + "/order/list",
      data: {
        openId: wx.getStorageSync('openId'),
        isPhoto: 0
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          array: res.data.data
        })
        if (that.data.array.length <= 0) {
          that.setData({
            empty : true,
            unempty : false
          })
        } else {
          that.setData({
            empty: false,
            unempty: true
          })
        }
      },
      complete: function (res) {
        wx.hideLoading()
      }
    })
    // 摄影订单
    wx.request({
      url: host + "/order/list",
      data: {
        openId: wx.getStorageSync('openId'),
        isPhoto: 1
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          arrayphoto: res.data.data
        })
        if (that.data.arrayphoto.length <= 0) {
          that.setData({
            photoempty: true,
            photounempty: false
          })
        } else {
          that.setData({
            photoempty: false,
            photounempty: true
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    this.refresh()
  },
  deleteOrder: function (e) {
    var that = this
    wx.showModal({
      title: '温馨提示',
      content: '您确定要删除该订单吗？',
      success: function (res) {
        if (res.confirm) {
          var id = e.currentTarget.dataset.id
          var host = app.globalData.host
          wx.request({
            url: host + "/order/delete",
            data: {
              openId: wx.getStorageSync('openId'),
              id: id
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              that.refresh()
            }
          })     
        } else if (res.cancel) {
        }
      }
    })

    
  },
  topay: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var host = app.globalData.host

    wx.setStorageSync("orderId", id)
    wx.request({
      url: host + "/order/payByOrder",
      data: {
        openId: wx.getStorageSync('openId'),
        id:id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        var source = res.data.data
        wx.requestPayment({
          'timeStamp': source.timeStamp,
          'nonceStr': source.nonce_str,
          'package': source.package,
          'signType': source.signType,
          'paySign': source.paySign,
          'success': function (res) {
            wx.showToast({
              title: '付款成功',
              icon: 'success',
              duration: 2000
            })
          },
          'fail': function (res) {
          },
          'complete':function(res){
            var id = wx.getStorageSync("orderId")
            wx.removeStorageSync("orderId")
            wx.redirectTo({
              url: '../orderdetail/orderdetail?id='+id,
            })           
          }
      })
    }
    })
  },
  // 订单为空时
  tohome:function(){
    wx.switchTab({
      url: '../index/index',
    })
  },
  // 确认收货
  tosureorder:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    wx.request({
      url: host + '/order/update',
      header: {'content-type': 'application/json'},
      data: {
        openId: wx.getStorageSync('openId'),
        id: id,
        status:70
      },
      success:function(res){
        wx.showToast({
          title: '确认收货成功',
          icon: 'success',
          duration: 2000
        })
      },
      complete:function(res){
        console.log(res)
        that.refresh()
      }
    })
  }
})